﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class TreatasDAO
    {
        public static List<TreatasDTO> getAll()
        {
            List<TreatasDTO> result = new List<TreatasDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TREATAs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                TreatasDTO dto = new TreatasDTO();

                dto.MaTreatAs = ds.MaTreatAs;
                dto.TreatAsName = ds.TreatAsName;             

                result.Add(dto);
            }

            return result;
        }

        public static TreatasDTO getAlongCode(int maTreatas)
        {
            TreatasDTO result = new TreatasDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TREATAs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.MaTreatAs == maTreatas).Single();

            result.MaTreatAs = query.MaTreatAs;
            result.TreatAsName = query.TreatAsName; 

            return result;
        }

        public static void Insert(TreatasDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TREATA treat = new TREATA();
            treat.TreatAsName = _insert.TreatAsName;
            treat.Deleted = _insert.Deleted;
            
            dbRaoVat.TREATAs.InsertOnSubmit(treat);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maTreatas)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TREATA _delete = (from n in dbRaoVat.TREATAs
                             where n.MaTreatAs == maTreatas && n.Deleted == 0
                             select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(TreatasDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TREATA _update = (from n in dbRaoVat.TREATAs
                              where n.MaTreatAs == dto.MaTreatAs && n.Deleted == 0
                              select n).Single();

            _update.TreatAsName = dto.TreatAsName;
            
            dbRaoVat.SubmitChanges();
        }
    }
}
